//
//  BCConversionController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2019/1/4.
//  Copyright © 2019 guoshikeji. All rights reserved.
//

#import "BCConversionController.h"

//cell
#import "BCConversionCurrencyHeaderView.h"
#import "BCConversionCurrencyCell.h"

//
#import "BConversionCellModel.h"

//request
#import "BCConversionRequest.h"

//
#import "BConversionSelectTickerCurrencyController.h"

//
@interface BCConversionController () <BConversionInputViewDelegate,UITableViewDataSource,UITableViewDelegate,BCConversionCurrencyHeaderViewDelegate,BConversionSelectTickerCurrencyControllerDelegate> {
    //输入视图高度
    CGFloat _inputViewHeight;
}


/**
 *BCConversionData
 */
@property (nonatomic, strong) BCConversionData *conversionData;
//
/**
 *当前输入的兑换数量
 */
@property (nonatomic, copy) NSString *conversionAmount;


/**
 *cellSource
 */
@property (nonatomic, strong) NSMutableArray *cellSource;

@end

@implementation BCConversionController

- (NSMutableArray *)cellSource {
    if (!_cellSource) {
        _cellSource = [NSMutableArray array];
        //
        [_cellSource addObjectsFromArray:[BConversionCellModel dataSource]];
    }
    return _cellSource;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //
    self.title = BCLocalizedKey(@"Conversion");
    //
    [self setupSubviews];
    [self setupLayoutSubviews];
    [self.view layoutIfNeeded];
    //
    [self requestDataCurr:nil init:true change:false];
}

- (void)setupSubviews {
    self.currencyView = [BConversionCurrencyView new];
    self.currencyView.alpha = 0;
    [self.view addSubview:self.currencyView];
    
    //
    self.conversionTable = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.conversionTable.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.conversionTable.dataSource = self;
    self.conversionTable.delegate = self;
    self.conversionTable.backgroundColor = [UIColor qmui_colorWithHexString:@"#F6F5FA"];
    [self.view addSubview:self.conversionTable];
    self.conversionTable.layer.cornerRadius = 4.0;
    self.conversionTable.layer.masksToBounds = true;
    //
    [self.conversionTable registerClass:BCConversionCurrencyHeaderView.class forHeaderFooterViewReuseIdentifier:NSStringFromClass(BCConversionCurrencyHeaderView.class)];
    [self.conversionTable registerClass:BCConversionCurrencyCell.class forCellReuseIdentifier:NSStringFromClass(BCConversionCurrencyCell.class)];
    NSMutableArray *registerCells = [BConversionCellModel registerCells];
    for (NSString *cellName in registerCells) {
        [self.conversionTable registerClass:NSClassFromString(cellName) forCellReuseIdentifier:cellName];
    }
    
    //输入视图
    self.conversionInputView = [BConversionInputView new];
    self.conversionInputView.needSafeBottomArea = true;
    self.conversionInputView.delegate = self;
    [self.view addSubview:self.conversionInputView];
}
- (void)setupLayoutSubviews {
    CGFloat width = CGRectGetWidth(self.view.frame);
    //
    CGFloat widthBi = ScreenWidth/375.0>1?1:ScreenWidth/375.0;
    [self.currencyView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.customNaviBar.mas_bottom).offset(15.0);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(self.view).offset(-20.0);
        make.height.mas_equalTo(50.0*widthBi);
    }];
    
    //输入视图约束
    CGSize inputViewSize = [BConversionInputView sizeFromWidth:width needSafeBottomArea:self.conversionInputView.needSafeBottomArea];
    _inputViewHeight = inputViewSize.height;
    //
    CGFloat offset = _inputViewHeight;
    [self.conversionInputView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view);
        make.size.mas_equalTo(inputViewSize);
        make.bottom.mas_equalTo(self.view).offset(offset);
    }];
    
    //
    [self.conversionTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.currencyView.mas_bottom).offset(15.0);
        make.centerX.mas_equalTo(self.view);
        make.width.mas_equalTo(self.currencyView);
        make.height.mas_equalTo(0);
    }];
}



- (void)showInputViewAction {
    CGFloat offset = 0;
    [self.conversionInputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.view).offset(offset);
    }];
    [UIView animateWithDuration:0.2 animations:^{
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self layoutConversionTable];
    }];
}

/**
 设置当前兑换数量
 */
- (void)setConversionAmount:(NSString *)conversionAmount {
    _conversionAmount = conversionAmount;
    //设置数量
    self.currencyView.currencyAmount = conversionAmount;
    //刷新table
    if (self.conversionData) {
        self.conversionData.conversionAmount = conversionAmount;
    }
    [self.conversionTable reloadData];
    [self layoutConversionTable];
}

/**
 重新设置table的高度
 */
- (void)layoutConversionTable {
    //table的contentSize
//    CGSize contentSize = self.conversionTable.contentSize;
    
    //table的最大高度
    CGFloat maxHeight = CGRectGetMinY(self.conversionInputView.frame)-CGRectGetMaxY(self.currencyView.frame)-30.0f;
    NSLog(@"%f",maxHeight);
    
    [self.conversionTable mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(maxHeight);
    }];
}



#pragma mark - BConversionInputViewDelegate
- (void)conversionInputViewDidInputString:(NSString *)string {
    //当前
    NSString *conversionAmount = self.conversionAmount;
    self.conversionAmount = [BConversionInputView string:conversionAmount byAppendingString:string digits:7];
}
#pragma mark - UITableViewDataSource,UITableViewDelegate
//
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    BCConversionCurrencyHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:NSStringFromClass(BCConversionCurrencyHeaderView.class)];
    headerView.contentView.backgroundColor = tableView.backgroundColor;
    headerView.delegate = self;
    headerView.conversionData = self.conversionData;
    return headerView;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return [BCConversionCurrencyHeaderView defaultHeight];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0;
}
//
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.conversionData) {
        return self.cellSource.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BConversionCellModel *cellModel = self.cellSource[indexPath.row];
    BCConversionCurrencyCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(cellModel.cellClass)];
    cell.cellModel = cellModel;
    cell.conversionData = self.conversionData;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BConversionCellModel *cellModel = self.cellSource[indexPath.row];
    BCConversionData *data = self.conversionData;
    return [tableView fd_heightForCellWithIdentifier:NSStringFromClass(cellModel.cellClass) configuration:^(id cell) {
        ((BCConversionCurrencyCell *)cell).cellModel = cellModel;
        ((BCConversionCurrencyCell *)cell).conversionData = data;
    }];
}


#pragma mark - request
- (void)requestDataCurr:(id)curr init:(BOOL)init change:(BOOL)change {
    if (init) {
        [self.view bc_showLoadingInsets:UIEdgeInsetsMake(CGRectGetHeight(self.customNaviBar.frame), 0, 0, 0)];
    }
    BCConversionRequest *request = [BCConversionRequest new];
    //
    WEAK_SELF
    [request didRequestCompleted:^{
        [wself.view bc_hiddenLoading];
    }];
    [request didRequestSuccessed:^(id result, id page) {
        BCConversionResponse *resp = result;
        [wself handleData:resp.data init:init change:change];
    }];
    [request didRequestFailed:^(NSError *error) {
        if (error) {
            if (init) {
                [wself.view bc_showWithInsets:UIEdgeInsetsMake(CGRectGetHeight(wself.customNaviBar.frame), 0, 0, 0) error:error needRefresh:true refreshTitle:nil target:wself refreshAction:@selector(requestInitFailedAction)];
            }
        }
    }];
    [request requestServer];
}
- (void)requestInitFailedAction {
    [self.view bc_hiddenError];
    [self requestDataCurr:nil init:true change:false];
}
//
- (void)handleData:(BCConversionData *)respData init:(BOOL)init change:(BOOL)change {
    if (respData) {
        self.conversionData = respData;
        //显示视图
        self.currencyView.alpha = 1;
        //
        self.currencyView.conversionData = self.conversionData;
        //
        self.conversionAmount = _conversionAmount;
        
        //
        [self showInputViewAction];
    }
}
//
#pragma mark - BCConversionCurrencyHeaderViewDelegate
- (void)conversionCurrencyHeaderViewCurrencyButtonDidClickedAction {
    BConversionSelectTickerCurrencyController *selectVC = [BConversionSelectTickerCurrencyController new];
    selectVC.conversionData = self.conversionData;
    selectVC.delegate = self;
    [self.navigationController presentViewController:selectVC animated:true completion:nil];
}
- (void)conversionSelectTickerCurrencyControllerDidSelectModel:(BCConversionDataTickerModel *)model {
    self.conversionData.currentTicker = model;
    self.conversionAmount = _conversionAmount;
}

@end
